﻿public class Solution {
    public int WiggleMaxLength(int[] nums)
    {
        // 处理特殊情况
        if (nums is null || nums.Length == 0) return 0;
        if (nums.Length == 1) return 1;
        if (nums.Length == 2 && nums[0] != nums[1]) return 2;

        int curDiff = 0;
        int preDiff = 0;
        int result = 1;

        for (int i = 0; i < nums.Length - 1; i++)
        {
            curDiff = nums[i + 1] - nums[i];
            if ((preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0))
            {
                preDiff = curDiff;
                result++;
            }
        }
        return result;
    }

    public static void Main(string[] args)
    {
        int[] nums = [1, 17, 5, 10, 13, 15, 10, 5, 16, 8];
        Solution solution = new Solution();
        int result = solution.WiggleMaxLength(nums);
        Console.WriteLine(result);
    }
}